<template>
  <el-dropdown-item icon="el-icon-edit" @click.native="dialogVisible = true">修改信息
    <el-dialog title="修改信息" :visible.sync="dialogVisible" append-to-body width="30%" :close-on-click-modal="false">
      <el-form ref="form" :model="form">
        <el-form-item prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio :label="0" border>女</el-radio>
            <el-radio :label="1" border>男</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </el-dropdown-item>
</template>

<script>
import Cookies from "js-cookie";
import { validatePassword } from "@/utils/validate";
import { updateUser } from "@/api/auth/user";

export default {
  name: "EditUserInfo",
  inject: ["getUserInfo"],
  data() {
    return {
      dialogVisible: false,
      user: {},
      form: {},
      sex: ['女', '男']
    }
  },
  methods: {
    edit() {
      updateUser(this.form).then(res => {
        if(res.data.code === 200) {
          this.user.nickname = this.form.nickname
          this.user.sex = this.form.sex
          Cookies.set("user", JSON.stringify(this.user))
          this.getUserInfo()
          this.$message.success(res.data.message)
          this.dialogVisible = false
        } else {
          // this.$message.error(res.data.message)
        }
      })
    }
  },
  created() {
    this.user = JSON.parse(Cookies.get("user"))
    this.form = {
      id: this.user.id,
      nickname: this.user.nickname,
      sex: this.user.sex
    }
  }
}
</script>
